package com.yiguantong.police.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnKeyListener;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.MainApplication;
import com.yiguantong.police.R;


/**
 * 对话框工具类 类名称：DialogUtil 作者： wanglinqi 版权声明 : Copyright (C) 2012-2014 南京欣网
 * 修改时间：2013-5-15 下午4:05:59
 */
public class DialogUtil {
    /**
     * 对话框参数类
     * <p/>
     * 类名称：DialogParams
     * <p/>
     * 作者： wanglinqi
     * <p/>
     * 版权声明 : Copyright (C) 2012-2014 南京欣网
     * <p/>
     * 修改时间：2013-5-15 下午4:21:22
     */
    public static class DialogParams {
        /**
         * 确定按钮文字
         */
        private String confirmButtonText;

        /**
         * 取消按钮文字
         */
        private String cancelButtonText;

        /**
         * 消息
         */
        private String messageText;
        /**
         * 内容是否居中
         */
        private boolean isMessageAlignCenter;

        /**
         * 标题
         */
        private String title;

        /**
         * 是否显示标题
         */
        private boolean isTitleVisible = true;

        /**
         * 确认事件
         */
        private OnClickListener confirmListener;

        /**
         * 取消事件
         */
        private OnClickListener cancelListener;

        /**
         * 返回键事件
         */
        private OnKeyListener onKeyListener;

        /**
         * 为了使用方便,增加参数对应的对话框
         */
        private Dialog dialog;

        /**
         * 方法描述：构造方法
         *
         * @author wanglinqi
         * <p/>
         * 备注:
         */
        public DialogParams() {
        }

        /**
         * <p/>
         * 方法描述：构造方法
         *
         * @param args {1:message 2:cancelButtonText 3:confirmButtonText 4:title}
         *             <p/>
         *             备注:
         * @author wanglinqi
         */

        public DialogParams(String... args) {
            try {
                this.isTitleVisible = true;
                this.messageText = args[0];
                this.cancelButtonText = args[1];
                this.confirmButtonText = args[2];
                this.title = args[3];
            } catch (Exception exp) {
                // 不考虑越界异常，初始化参数
            }
        }

        public DialogParams(boolean isTitleVisible, String... args) {
            try {
                this.isTitleVisible = isTitleVisible;
                this.messageText = args[0];
                this.cancelButtonText = args[1];
                this.confirmButtonText = args[2];
                this.title = args[3];
            } catch (Exception exp) {
                // 不考虑越界异常，初始化参数
            }
        }

        public DialogParams(boolean isTitleVisible, boolean isCenter, String... args) {
            try {
                this.isTitleVisible = isTitleVisible;
                this.messageText = args[0];
                this.cancelButtonText = args[1];
                this.confirmButtonText = args[2];
                this.title = args[3];
            } catch (Exception exp) {
                // 不考虑越界异常，初始化参数
            }
        }

        public DialogParams setMsg(String msg) {
            this.messageText = msg;
            return this;
        }

        public DialogParams setMessageAlignCenter(boolean isMessageAlignCenter) {
            this.isMessageAlignCenter = isMessageAlignCenter;
            return this;
        }

        public DialogParams setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 确认事件 方法名称：setConfirmListener
         *
         * @param confirmListener <p/>
         *                        备注：
         * @author wanglinqi
         */
        public void setConfirmListener(OnClickListener confirmListener) {
            this.confirmListener = confirmListener;
        }

        /**
         * 确认事件 方法名称：setConfirmListener
         *
         * @author wanglinqi
         */
        public void setKeyListener(OnKeyListener onKeyListener) {
            this.onKeyListener = onKeyListener;
        }

        /**
         * 取消事件 方法名称：setCancelListener
         *
         * @param concelListener <p/>
         *                       备注：
         * @author wanglinqi
         */
        public void setCancelListener(OnClickListener concelListener) {
            this.cancelListener = concelListener;
        }

        /**
         * 关闭对话框 方法名称：dismiss
         *
         * @author wanglinqi
         * <p/>
         * 备注：
         */
        public void dismiss() {
            DialogUtil.dismiss(dialog);
        }
    }

    /**
     * 初始化对话框参数 方法名称：initializeDialog
     *
     * @param context
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    public static Dialog initializeDialog(Context context, int layoutId) {
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(layoutId);
        Window window = dialog.getWindow();
        LayoutParams params1 = window.getAttributes();
        int width = MainApplication.getIns().getScreenWidth();
        float margin = 30 * MainApplication.getIns().getDensity() * 2;
        params1.width = Math.round(width - margin);
        window.setAttributes(params1);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    /**
     * @param context
     * @param layoutId
     * @param isTouchOut 触摸对话框外是否关闭对话框 true 关闭 false 不关闭
     * @param position   null 默认垂直居中左右有间距0 下方弹出左右无间距
     * @return
     */
    public static Dialog initializeDialog(Context context, int layoutId, Integer position, Boolean isTouchOut) {

        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(layoutId);
        Window window = dialog.getWindow();
        int width = MainApplication.getIns().getScreenWidth();
        LayoutParams params1 = window.getAttributes();
        if (null != position && 0 == position.intValue()) {
            window.setGravity(Gravity.BOTTOM);
            params1.width = width;
        } else {
            float margin = 30 * MainApplication.getIns().getDensity() * 2;
            params1.width = Math.round(width - margin);

        }
        window.setAttributes(params1);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    /**
     * 初始化一个按钮对话框 方法名称：initializeOneButtonDialog
     *
     * @param context
     * @param params
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    private static Dialog initializeOneButtonDialog(Context context, DialogParams params) {
        Dialog dialog = initializeDialog(context, R.layout.failure_dialog_layout);

        LinearLayout ll_title = (LinearLayout) dialog.findViewById(R.id.ll_one_title);
        ll_title.setVisibility(View.VISIBLE);
        if (params.isTitleVisible) {
            TextView title = (TextView) dialog.findViewById(R.id.d_one_title_tv);
            if (!TextUtils.isEmpty(params.title)) {
                title.setText(params.title);
            }
        } else {
            ll_title.setVisibility(View.GONE);
        }
        TextView msg = (TextView) dialog.findViewById(R.id.message01);
        msg.setText(params.messageText);
        TextView button = (TextView) dialog.findViewById(R.id.confirmButton01);
        if (params.confirmListener != null) {
            button.setOnClickListener(params.confirmListener);
        }
        params.dialog = dialog;
        dialog.setOnKeyListener(params.onKeyListener);
        return dialog;
    }

    /**
     * 初始化两个按钮对话框 方法名称：initializeTwoButtonDialog
     *
     * @param context
     * @param params
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    private static Dialog initializeTwoButtonDialog(Context context, DialogParams params) {
        Dialog dialog = initializeDialog(context, R.layout.dialog_layout);

        LinearLayout ll_title = (LinearLayout) dialog.findViewById(R.id.ll_two_title);
        ll_title.setVisibility(View.VISIBLE);
        if (params.isTitleVisible) {
            TextView title = (TextView) dialog.findViewById(R.id.d_two_title_tv);
            if (!TextUtils.isEmpty(params.title)) {
                title.setText(params.title);
            }
        } else {
            ll_title.setVisibility(View.GONE);
        }

        dialog.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);
        TextView confirmButton = (TextView) dialog.findViewById(R.id.confirmButton);
        TextView cancelButton = (TextView) dialog.findViewById(R.id.cancelButton);
        confirmButton.setText(params.confirmButtonText);
        cancelButton.setText(params.cancelButtonText);
        TextView msg = (TextView) dialog.findViewById(R.id.message);
        msg.setText(params.messageText);
        if (params.isMessageAlignCenter) {
            if (Build.VERSION.SDK_INT > 16) {
                msg.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            } else {
                msg.setGravity(View.TEXT_ALIGNMENT_CENTER);
            }
        }
        if (params.confirmListener != null) {
            confirmButton.setOnClickListener(params.confirmListener);
        }
        if (params.cancelListener != null) {
            cancelButton.setOnClickListener(params.cancelListener);
        }
        params.dialog = dialog;
        dialog.setOnKeyListener(params.onKeyListener);
        return dialog;
    }

    /**
     * 显示加载圈圈
     * <p/>
     * 方法名称：showLoadingDialog
     *
     * @param context
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    public static Dialog showLoadingDialog(Context context, int textId) {
        Dialog dialog = createLoadingDialog(context, textId);
        try {
            // dialog从初始化到show的过程中,如果界面销毁,将会导致异常
            dialog.show();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return dialog;
    }

    public static Dialog createLoadingDialog(Context context, int textId) {
        if (context == null) {
            return null;
        }
        View view = LayoutInflater.from(context).inflate(R.layout.loading_layout, null);
        final TextView loadingTV = (TextView) view.findViewById(R.id.loading_tv);
        loadingTV.setText(context.getString(textId));
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        LayoutParams params1 = window.getAttributes();
        int width = MainApplication.getIns().getScreenWidth();
        float margin = 30 * MainApplication.getIns().getDensity() * 2;
        params1.width = Math.round(width - margin);
        window.setAttributes(params1);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);
        return dialog;
    }


    public static Dialog showLoadingDialog(Context context) {
        return showLoadingDialog(context, R.string.loading_txt);
    }

    /**
     * 显示一个 按钮对话框
     * <p/>
     * 方法名称：showOneButtonDialog
     *
     * @param context
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    public static Dialog showOneButtonDialog(Context context, DialogParams params) {
        if (context == null || params == null) {
            return null;
        }
        Dialog dialog = initializeOneButtonDialog(context, params);
        try {
            // dialog从初始化到show的过程中,如果界面销毁,将会导致异常
            dialog.show();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return dialog;
    }


    /**
     * 显示两个按钮对话框
     * <p/>
     * 方法名称：showTwoButtonDialog
     *
     * @param context
     * @return <p/>
     * 备注：
     * @author wanglinqi
     */
    public static Dialog showTwoButtonDialog(Context context, DialogParams params) {
        if (context == null || params == null) {
            return null;
        }
        Dialog dialog = initializeTwoButtonDialog(context, params);
        try {
            // dialog从初始化到show的过程中,如果界面销毁,将会导致异常
            dialog.show();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return dialog;
    }


    public static Dialog showPermissionDialog(Context context, DialogParams params) {
        if (context == null || params == null) {
            return null;
        }
        Dialog dialog = initializeDialog(context, R.layout.dialog_layout_second_button);
        TextView content = (TextView) dialog.findViewById(R.id.dialog_content);
        TextView cancle = (TextView) dialog.findViewById(R.id.dialog_cancle);
        TextView open = (TextView) dialog.findViewById(R.id.dialog_open);

        content.setText(params.messageText);
        open.setText(params.confirmButtonText);
        cancle.setText(params.cancelButtonText);
        if (params.isMessageAlignCenter) {
            if (Build.VERSION.SDK_INT > 16) {
                content.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            } else {
                content.setGravity(View.TEXT_ALIGNMENT_CENTER);
            }
        }
        if (params.confirmListener != null) {
            open.setOnClickListener(params.confirmListener);
        }
        if (params.cancelListener != null) {
            cancle.setOnClickListener(params.cancelListener);
        }
        params.dialog = dialog;
        dialog.setOnKeyListener(params.onKeyListener);
        return dialog;
    }

    /**
     * 消灭对话框
     * <p/>
     * 方法名称：dismiss
     *
     * @param dialog <p/>
     *               备注：
     * @author wanglinqi
     */
    public static void dismiss(Dialog dialog) {
        if (dialog == null) {
            return;
        }
        try {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            // 防止有线程延迟操作导致的IllegalArgumentException
        }
    }

    /**
     * 非继承baseActivity新的等待框
     *
     * @param loadingDialog_d
     * @param mContext
     * @param text
     * @return
     */
    public static LoadingDialog showMLoadingDialog(LoadingDialog loadingDialog_d, Context mContext, String text) {
        if (loadingDialog_d == null) {
            loadingDialog_d = new LoadingDialog(mContext);
        }
        if (!loadingDialog_d.isShowing()) {
            loadingDialog_d.show();
            loadingDialog_d.setmText(text);
        }
        return loadingDialog_d;
    }

    /**
     * 非继承baseActivity新的等待框消失
     *
     * @param loadingDialog
     */
    public static void isNotActivityDialogDismiss(LoadingDialog loadingDialog) {
        if (loadingDialog == null) {
            return;
        }
        try {
            if (loadingDialog.isShowing() && loadingDialog != null) {
                loadingDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
